package com.ruoyi.kjcg.gfbzck.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.kjcg.gfbzck.domain.GfbzckFj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.kjcg.gfbzck.domain.Gfbzck;
import com.ruoyi.kjcg.gfbzck.service.IGfbzckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工法编制参考Controller
 *
 * @author huacai
 * @date 2024-11-02
 */
@RestController
@RequestMapping("/kjcg/gfbzck")
public class GfbzckController extends BaseController {
    @Resource
    private IGfbzckService gfbzckService;

    /**
     * 查询工法编制参考列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:list')")
    @GetMapping("/list")
    public TableDataInfo list(Gfbzck gfbzck) {
        startPage();
        List<Gfbzck> list = gfbzckService.selectGfbzckList(gfbzck);
        return getDataTable(list);
    }

    /**
     * 导出工法编制参考列表
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:export')")
    @Log(title = "工法编制参考", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Gfbzck gfbzck) {
        List<Gfbzck> list = gfbzckService.selectGfbzckList(gfbzck);
        ExcelUtil<Gfbzck> util = new ExcelUtil<Gfbzck>(Gfbzck.class);
        util.exportExcel(response, list, "工法编制参考数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Gfbzck> util = new ExcelUtil<Gfbzck>(Gfbzck.class);
        util.importTemplateExcel(response, "工法编制参考数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "工法编制参考", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Gfbzck> util = new ExcelUtil<Gfbzck>(Gfbzck.class);
        InputStream inputStream = file.getInputStream();
        List<Gfbzck> list = util.importExcel(inputStream);
        inputStream.close();
        int count = gfbzckService.batchInsertGfbzck(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取工法编制参考详细信息
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:query')")
    @GetMapping(value = "/{gfbzckId}")
    public AjaxResult getInfo(@PathVariable("gfbzckId") String gfbzckId) {
        return success(gfbzckService.selectGfbzckByGfbzckId(gfbzckId));
    }

    /**
     * 新增工法编制参考
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:add')")
    @Log(title = "工法编制参考", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Gfbzck gfbzck) {
        gfbzck.setUserId(Math.toIntExact(getUserId())); //用户ID
        gfbzck.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        gfbzck.setCreateBy(getUsername()); //创建人
        gfbzck.setIdWhere("存在");
        return toAjax(gfbzckService.insertGfbzck(gfbzck));
    }

    /**
     * 修改工法编制参考
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:edit')")
    @Log(title = "工法编制参考", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Gfbzck gfbzck) {
        return toAjax(gfbzckService.updateGfbzck(gfbzck));
    }

    /**
     * 删除工法编制参考
     */
    @PreAuthorize("@ss.hasPermi('kjcg:gfbzck:remove')")
    @Log(title = "工法编制参考", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gfbzckIds}")
    public AjaxResult remove(@PathVariable String[] gfbzckIds) {
        return toAjax(gfbzckService.deleteGfbzckByGfbzckIds(gfbzckIds));
    }

    /**
     * 获得工法编制参考主键ID (UUID)
     */
    @GetMapping("/getGfBzCkId")
    public AjaxResult getGfBzCkId() {
        String getGfBzCkId = IdUtils.fastSimpleUUID();
        return success(getGfBzCkId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertGfBzCkFjBygfbzckId(@RequestBody GfbzckFj gfbzckFj) {
        return toAjax(gfbzckService.insertGfBzCkFjBygfbzckId(gfbzckFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectGfBzCkFjList(String sId) {
        GfbzckFj gfbzckFj = new GfbzckFj();
        gfbzckFj.setGfbzckId(sId);
        return AjaxResult.success(gfbzckService.selectGfBzCkFjList(gfbzckFj));
    }

    /**
     * 删除工法编制参考附件
     */
    @Log(title = "工法编制参考附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteGfbzckFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(gfbzckService.deleteGfbzckFjByFjIds(fjIds));
    }
}
